var e = require("../../../wxParse/wxParse"), t = getApp();

Page({
    data: {
        payType: 0,
        explainFlag: !1,
        agreement: !1,
        categorys: [],
        categoryIndex: null,
        circleFace: "https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/circleMoRenFace.png",
        circleBg: "https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/circleMoRenBg.png",
        name: "",
        synopsis: "",
        price: 0,
        circleId: 0,
        circle_control: ""
    },
    onLoad: function(e) {
        this.getCategorys(), this.getCircleSet(), e.circleId && 0 != e.circleId && this.setData({
            circleId: e.circleId
        });
    },
    setPayType: function(e) {
        var t = e.currentTarget.dataset.type;
        this.setData({
            payType: t
        });
    },
    onChangeAgreement: function(e) {
        this.setData({
            agreement: e.detail
        });
    },
    setExplainFlag: function() {
        this.setData({
            explainFlag: !this.data.explainFlag
        });
    },
    bindPickerChange: function(e) {
        this.setData({
            categoryIndex: e.detail.value
        }), console.log(e.detail.value);
    },
    getCategorys: function() {
        var e = this, a = t.getNetAddresss("plugin.circle.frontend.circle.get-category-list");
        t._postNetWork({
            url: a,
            data: {},
            success: function(a) {
                var i = a.data;
                if (1 != i.result) return t.tips(i.msg);
                e.setData({
                    categorys: i.data
                }), 0 != e.data.circleId && e.getCircleInfo();
            }
        });
    },
    getCircleInfo: function() {
        var e = this, a = t.getNetAddresss("plugin.circle.frontend.circle.get-circle");
        t._postNetWork({
            url: a,
            data: {
                circle_id: this.data.circleId
            },
            success: function(a) {
                var i = a.data;
                if (1 != i.result) return t.tips(i.msg);
                var c = 0;
                e.data.categorys.forEach(function(e, t) {
                    e.id == i.data.category_id && (c = t);
                }), console.log(i.data), e.setData({
                    name: i.data.name,
                    categoryIndex: c,
                    synopsis: i.data.synopsis,
                    payType: i.data.is_toll,
                    price: i.data.price,
                    circleFace: i.data.circle_img,
                    circleBg: i.data.bg_img
                });
            }
        });
    },
    onReady: function() {},
    onShow: function() {},
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    submit: function() {
        var e = this.data, a = e.agreement, i = e.name, c = e.categorys, r = e.categoryIndex, n = e.synopsis, s = e.payType, o = e.price, l = e.circleFace, g = e.circleBg;
        if (0 == a) return t.tips("请阅读用户协议");
        if (t._isTextEmpty(i)) return t.tips("请填写圈子名称");
        if (null == r) return t.tips("请选择圈子分类");
        if (t._isTextEmpty(n)) return t.tips("请填写圈子简介");
        if (1 == s && 0 == o) return t.tips("请输入付费金额");
        var d = {
            is_toll: s,
            name: i,
            category_id: c[r].id,
            price: o,
            synopsis: n,
            bg_img: g,
            circle_img: l
        }, u = t.getNetAddresss("plugin.circle.frontend.circle.create-circle");
        0 != this.data.circleId && (d.circle_id = this.data.circleId, u = t.getNetAddresss("plugin.circle.frontend.circle.edit-circle")), 
        t._postNetWork({
            url: u,
            data: d,
            success: function(e) {
                var a = e.data;
                if (t.tips(a.msg), 1 != a.result) return t.tips(a.msg);
                wx.navigateBack();
            }
        });
    },
    getCircleSet: function() {
        var a = this, i = t.getNetAddresss("plugin.circle.frontend.circle.get-set");
        t._getNetWork({
            url: i,
            data: {},
            success: function(i) {
                var c = i.data;
                if (1 != c.result) return t.tips(c.msg);
                var r = c.data.circle_control;
                try {
                    r.indexOf("&#") <= -1 && e.wxParse("article", "html", r, a);
                } catch (e) {
                    console.log(e);
                }
            },
            fail: function(e) {
                console.log(e);
            }
        });
    },
    editImage: function(e) {
        var t = this, a = e.currentTarget.dataset.type;
        this.upImage(function(e) {
            "face" == a ? t.setData({
                circleFace: e.data.img_url
            }) : "bg" == a && t.setData({
                circleBg: e.data.img_url
            });
        });
    },
    upImage: function(e) {
        var a = t.getNetAddresss("upload.uploadPic");
        wx.chooseImage({
            count: 1,
            sizeType: [ "original", "compressed" ],
            sourceType: [ "album", "camera" ],
            success: function(i) {
                var c = i.tempFilePaths[0];
                wx.uploadFile({
                    url: a,
                    filePath: c,
                    name: "file",
                    success: function(a) {
                        var i = JSON.parse(a.data);
                        1 == i.result ? e(i) : t.tips(i.msg);
                    }
                });
            }
        });
    },
    onShareTimeline: function() {},
    onShareAppMessage: function() {}
});